/*
 *  $URL$
 *  $Rev$
 *  $Date$
 *  $Id$
 *  Copyright 2009 Interactive Media Management.
 */
package com.interactivemediamanagement.converge.web.dav;

import com.bradmcevoy.http.CollectionResource;
import com.bradmcevoy.http.FolderResource;
import com.bradmcevoy.http.Range;
import com.bradmcevoy.http.Request;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.io.StreamToStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Allan Lykke Christensen
 */
public class NewsItemFolderResource extends NewsItemResource
        implements FolderResource {

    ArrayList<NewsItemResource> children = new ArrayList<NewsItemResource>();

    public NewsItemFolderResource(NewsItemFolderResource parent, String name) {
        super(parent, name);
    }

    @Override
    public Long getContentLength() {
        return null;
    }

    public String getContentType() {
        return "text/html";
    }

    @Override
    public String checkRedirect(Request request) {
        return null;
    }

    public List<? extends Resource> getChildren() {
        return children;
    }

    @Override
    public Long getMaxAgeSeconds() {
        return (long) 10;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        NewsItemFolderResource r = new NewsItemFolderResource(getParent(), getName());
        for (NewsItemResource child : children) {
            child.clone(r); // cstr adds to children
        }
        return r;
    }

    public CollectionResource createCollection(String newName) {
        NewsItemFolderResource r = new NewsItemFolderResource(this, newName);
        return r;
    }

    public Resource child(String childName) {
        for (Resource r : getChildren()) {
            if (r.getName().equals(childName)) {
                return r;
            }
        }
        return null;
    }

    @Override
    public void sendContent(OutputStream out, Range range,
            Map<String, String> params) throws IOException {
        PrintWriter printer = new PrintWriter(out, true);
        printer.print("<html><body>");
        printer.print("<h1>" + getName() + "</h1>");
        printer.print("<ul>");
        for (NewsItemResource r : children) {
            printer.print("<li><a href='" + r.getHref() + "'>" + r.getName() + "</a></li>");
        }
        printer.print("</ul>");

        printer.print("</body></html>");
        printer.flush();
    }

    public Resource createNew(String newName, InputStream inputStream, Long length, String contentType) throws
            IOException {
        ByteArrayOutputStream bos = readStream(inputStream);
        NewsItemResource r = new NewsItemBinaryResource(this, newName, bos.toByteArray());
        return r;
    }

    public void copyTo(CollectionResource toCollection, String name) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void moveTo(CollectionResource rDest, String name) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private ByteArrayOutputStream readStream(final InputStream in) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        StreamToStream.readTo(in, bos);
        return bos;
    }
}
